//找到所有红色表格的td
var alltds = document.querySelectorAll("#boardHide td");

var turn = "black";

var round = document.getElementById("round");

var finish = false;


var his = [];
function tdClick(e) {
    // e.target.style.backgroundColor = "red";
    if (finish) {
        return;
    }
    var td = e.target;
    if (td.getAttribute("chess")) {
        return;
    }
    td.setAttribute("chess", turn);

    winCheck(e.target.index);



    if (turn == "black") {
        turn = "white";
        round.textContent = "白方回合";
    }
    else {
        turn = "black";
        round.textContent = "黑方回合";
    }

    his.push(e.target);

}

for (i = 0; i < alltds.length; i++) {

    alltds[i].onclick = tdClick;
    alltds[i].index = i;


}


function back() {
    finish = false;
    if (his.length > 0)
        var td = his.pop();
    td.removeAttribute("chess");
   if(turn =="black"){
       turn = "white";
       round.textContent = "白方回合";
    }
    else{
        tuen = "black";
        round.textContent = "黑方回合";
    }
   }

function restart() {
    window.location.reload();

}
//判断输赢
function winCheck(index) {
    //检查横向输赢
    if (horizontalCheck(index) || verticalCheck(index) || tlbrCheck(index) || ddCheck(index)) {
        finish = true;
        alert((turn == "black"?"黑":"白") + "方胜利！");
    }
}

function horizontalCheck(index) {
    var num = 1;
    var currentIndex = index;

    var hasNext = currentIndex % 19 != 0 && alltds[currentIndex - 1].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex--;
        hasNext = currentIndex % 19 != 0 && alltds[currentIndex - 1].getAttribute("chess") == turn;
    }
    //..........................................................

    var currentIndex = index;

    var hasNext = currentIndex % 19 != 18 && alltds[currentIndex + 1].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex++;
        hasNext = currentIndex % 19 != 18 && alltds[currentIndex + 1].getAttribute("chess") == turn;
    }
    return num >= 5;
}


//***************************************** */
function verticalCheck(index) {
    var num = 1;
    var currentIndex = index;

    var hasNext = currentIndex >= 19 && alltds[currentIndex - 19].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex -= 19;
        hasNext = currentIndex % 19 != 0 && alltds[currentIndex - 19].getAttribute("chess") == turn;
    }
    //..........................................................

    var currentIndex = index;

    var hasNext = currentIndex < 342 && alltds[currentIndex + 19].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex += 19;
        hasNext = currentIndex < 342 && alltds[currentIndex + 19].getAttribute("chess") == turn;
    }
    return num >= 5;
}

//左上-右下
function tlbrCheck(index) {
    var num = 1;
    var currentIndex = index;

    var hasNext = currentIndex % 19 != 0 && currentIndex >= 19 && alltds[currentIndex - 20].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex -= 20;
        hasNext = currentIndex % 19 != 0 && currentIndex >= 19 && alltds[currentIndex - 20].getAttribute("chess") == turn;
    }
    //..........................................................

    var currentIndex = index;

    var hasNext = currentIndex < 342 && currentIndex % 19 != 18 && alltds[currentIndex + 20].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex += 20;
        hasNext = currentIndex < 342 && currentIndex % 19 != 18 && alltds[currentIndex + 20].getAttribute("chess") == turn;
    }
    return num >= 5;
}

//右上到左下
function ddCheck(index) {
    var num = 1;
    var currentIndex = index;

    var hasNext = currentIndex % 19 != 18 && currentIndex >= 19 && alltds[currentIndex - 18].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex -= 18;
        hasNext = currentIndex % 19 != 18 && currentIndex >= 19 && alltds[currentIndex - 18].getAttribute("chess") == turn;
    }
    //..........................................................

    var currentIndex = index;

    var hasNext = currentIndex < 342 && currentIndex % 19 != 0 && alltds[currentIndex + 18].getAttribute("chess") == turn;

    while (hasNext) {
        num++;
        currentIndex += 18;
        hasNext = currentIndex < 342 && currentIndex % 19 != 0 && alltds[currentIndex + 18].getAttribute("chess") == turn;
    }
    return num >= 5;
}